﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.ShpMana
{
    public partial class wfUpdateRouteBaseinfo : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //修改路线
        private void UpdateRoute()
        {
            string routeId = this.GetParamHtValue("routeId");

            if (string.IsNullOrEmpty(routeId))
            {
                this.ExecJScript(@"layer.alert('该路线不存在.', 3);");
                return;
            }
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录时间过期，请重新登录.', 3);");
                return;
            }
            string routeTitle = this.GetParamHtValue("routeTitle");
            if (string.IsNullOrEmpty(routeTitle))
            {
                this.ExecJScript(@"layer.alert('标题不能为空.', 3);");
                return;
            }
            string routeJianjie = this.GetParamHtValue("routeJianjie");
            if (string.IsNullOrEmpty(routeJianjie))
            {
                this.ExecJScript(@"layer.alert('请填写路线介绍.', 3);");
                return;
            }

            string characteristic = this.GetParamHtValue("characteristic");
            if (string.IsNullOrEmpty(characteristic))
            {
                this.ExecJScript(@"layer.alert('请填写主要景点.', 3);");
                return;
            }

            string txtInfo = this.GetParamHtValue("txtInfo");
            if (string.IsNullOrEmpty(txtInfo))
            {
                this.ExecJScript(@"layer.alert('请填写简介.', 3);");
                return;
            }
            string ispush = this.GetParamHtValue("ispush");
            if (string.IsNullOrEmpty(ispush))
            {
                this.ExecJScript("layer.alert('请选择是否为推荐.', 3);");
                return;
            }

            string numberTemp = this.GetParamHtValue("numberDays");
            if (string.IsNullOrEmpty(numberTemp))
            {
                this.ExecJScript(@"layer.alert('请填写日程天数.', 3);");
                return;
            }
            int numberDays = 0;
            int.TryParse(numberTemp, out numberDays);
            if (numberDays < 1)
            {
                this.ExecJScript(@"layer.alert('日程天数必须大于0.', 3);");
                return;
            }

            string countryId = this.GetParamHtValue("countryId");

            string cityId = this.GetParamHtValue("cityId");
            string destId = this.GetParamHtValue("destinationId");
            if (cityId == "0" || countryId == "0")
            {
                this.ExecJScript(@"layer.alert('请选择地区.', 3);");
            }

            string routePrice = this.GetParamHtValue("routePrice");
            if (string.IsNullOrEmpty(routePrice))
            {
                this.ExecJScript(@"layer.alert('请填写价格.', 3);");
                return;
            }
            string djackList = this.GetParamHtValue("djackList");
            string shoutu = this.GetParamHtValue("shoutu");
            if (string.IsNullOrEmpty(shoutu))
            {
                this.ExecJScript(@"layer.alert('请选择首图.', 3);");
                return;
            }

            string countryName = this.GetParamHtValue("countryName");
            string cityName = this.GetParamHtValue("cityName");
            string destName = this.GetParamHtValue("destName");

            using (msBsc.AppLogic.DataAlc.CBizSysFile oCBizSysFile = new msBsc.AppLogic.DataAlc.CBizSysFile())
            {
                //保存到系统永久目录，之前上传时存到临时目录的，也可另写代码拷贝文件到其他目录
                shoutu = oCBizSysFile.SaveImgToServer("", msBsc.SvRef.UserInfo.Operator, "1", "VEH", ""
                    , shoutu//文件来源虚拟路径
                    , ""//文件来源物理路径，虚拟和物理只有一个就行
                    , true);
            }
            using (CBizDjkRouteBaseinfo route = new CBizDjkRouteBaseinfo())
            {
                string[] routeMessage = route.UpdateRoute(routeId,routeTitle, txtInfo, numberDays, countryId, cityId, destId, countryName, cityName, destName, characteristic, shoutu, routeJianjie, routePrice, Convert.ToInt16(ispush), userid);
                if (Convert.ToBoolean(routeMessage[0]))
                {
                    this.ExecJScript("success();");
                }
                else
                {
                    this.ExecJScript(@"layer.alert('" + routeMessage[1] + ".', 3);");
                }
            }

        }

        public void GetRouteInfo()
        {
            string routeId = this.GetParamHtValue("routeId");
            if (string.IsNullOrEmpty(routeId))
            {
                return;
            }
            using (CBizDjkRouteBaseinfo route = new CBizDjkRouteBaseinfo())
            {
                DataSet ds = route.GetRouteInfoByID(routeId);
                DataTable dt = ds.Tables[0];
                if (dt == null)
                {
                    return;
                }
                //加载 主线路信息
                string jsonInfo = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dt, "", "", 1, 2, "");
                this.ExecJScript("loadRouteInfo(" + jsonInfo + ")");
            }

        }

        public override void DoAction()
        {
            if (this.strAction == "updateRoute")
            {
                UpdateRoute();
            }
            else if (this.strAction == "changeArea")
            {
                RefreshArea();
            }
            else if (this.strAction == "init")
            {
                GetRouteInfo();
            }
        }

        private void RefreshArea()
        {
            string countryId = this.GetParamHtValue("countryId");
            string cityId = this.GetParamHtValue("cityId");

            DataSet ds1, ds2;

            using (msBsc.AppLogic.DataAlc.CBizCountryCity objCBizCountryCity = new AppLogic.DataAlc.CBizCountryCity())
            {
                ds1 = string.IsNullOrEmpty(countryId) ? null : objCBizCountryCity.SelectCountryByParentId(countryId);

                ds2 = string.IsNullOrEmpty(cityId) ? null : objCBizCountryCity.SelectCountryByParentId(cityId);
            }

            if (ds1 != null)
            {
                var countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds1.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#country_city_list').html();
                            laytpl(_html).render(" + countryJson + @", function (render) {
                                $('#selCity').html(render);
                            });");
            }

            if (ds2 != null)
            {
                var cityJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds2.Tables[0], "", "", 1, 200, "");
                this.ExecJScript(@"var _html = $('#city_des_list').html();
                            laytpl(_html).render(" + cityJson + @", function (render) {
                                $('#selDestination').html(render);
                            });");
            }
        }
    }
}